﻿using System;
using System.Configuration;
using System.IO;

namespace SZJT.Framework
{
    public static class LoggerHelper
    {
        [Flags]
        public enum DebugLevel
        {
            None = 0,
            Status = 1,
            Message = 2,
            StackTrace = 3,
            TransferData = 4,
            CodePoint = 5,
            AllInfo = 6
        }

        public static void WriteStatusInfo(string info)
        {
            WriteLog(info, DebugLevel.Status);
        }

        public static void WriteMessageInfo(string info)
        {
            WriteLog(info, DebugLevel.Message);
        }

        public static void WriteStackTraceInfo(string info)
        {
            WriteLog(info, DebugLevel.StackTrace);
        }

        public static void WriteTransferDataInfo(string info)
        {
            WriteLog(info, DebugLevel.TransferData);
        }

        public static void WriteCodePointInfo(string info)
        {
            WriteLog(info, DebugLevel.CodePoint);
        }

        //private static void WriteLog(string message, System.Diagnostics.TraceEventType severity, DebugLevel level)
        //{
        //    var debugLevel = Convert.ToString(ConfigurationManager.AppSettings["DebugLevel"]);
        //    if (string.IsNullOrEmpty(debugLevel)) return;
        //    if (!debugLevel.Equals(DebugLevel.AllInfo.ToString()))
        //        if (!debugLevel.Equals(level.ToString()))
        //            return;
        //    var logEntry = new LogEntry { Severity = severity, Message = message, TimeStamp = DateTime.Now };
        //    logEntry.Categories.Add(severity.ToString());
        //    //Logger.Write(logEntry);
        //}

        /// <summary>
        /// 是否需要调试数据
        /// </summary>
        static public DebugLevel Level
        {
            get
            {
                try
                {
                    var strDebugLevel = ConfigurationManager.AppSettings.Get("DebugLevel");
                    return strDebugLevel == null
                               ? DebugLevel.None
                               : (DebugLevel) Enum.Parse(typeof (DebugLevel), strDebugLevel);
                }
                catch
                {
                }
                return DebugLevel.None;
            }
        }

        /// <summary>
        /// 跟踪数据存储的文件位置
        /// </summary>
        static private string _logFileName;
        static public string LogFileName
        {
            get
            {
                if (_logFileName == null)
                    _logFileName = ConfigurationManager.AppSettings.Get("LogFileName");
                return _logFileName;
            }
            set
            {
                _logFileName = value;
            }
        }

        /// <summary>
        /// 写入跟踪信息
        /// </summary>
        /// <param name="strLogInfo">跟踪信息</param>
        static public void WriteLog(string strLogInfo, DebugLevel WriteLevel)
        {
            if (Level < WriteLevel)
                return;

            if (LogFileName != null)
            {
                try
                {
                    StreamWriter w;
                    if (LogFileName.IndexOf("{0}") >= 0)
                    {
                        DateTime t = DateTime.Now;
                        w = File.AppendText(string.Format(LogFileName, t.Year, t.Month, t.Day));
                    }
                    else
                        w = File.AppendText(LogFileName);

                    w.Write("{0} {1}\r\n", strLogInfo, DateTime.Now);
                    w.Close();
                }
                catch
                {
                }
            }
        }
    }
}
